using ZyGame.Avatar;
using ZyGame.Event;
using ZyGame.Execute;
using System.Runtime.InteropServices;
using UnityEngine;
using System.Collections.Generic;
using System;
using ZyGame.Drawing;
using ZyGame.Game;
using System.Xml.Linq;

namespace ZyGame
{
    class WebGLEventSubscribe : IEventHandle
    {
#if UNITY_WEBGL
        [DllImport("__Internal")]
        public static extern void OnNotiflyEvent(string name, string data);
#endif
        public void Clear(string evtName)
        {
        }

        public void Dispose()
        {
        }

        public void Notify(string evtName, object evtData)
        {
#if UNITY_WEBGL && !UNITY_EDITOR
            OnNotiflyEvent(evtName, evtData?.ToString());   
#endif
        }

        public void Register(string eventName, bool isOnce, Action<object> callback)
        {

        }

        public void Unregister(string eventName, Action<object> callback)
        {

        }
    }
    public class WebGLAvatar : MonoBehaviour
    {
        public bool openLog;

        public void Awake()
        {
            Client.Console.WriteLine(Application.version);
            Client.Events.Subscribe<WebGLEventSubscribe>();
            DrawingSetting.instance.Initialized();
            Debug.unityLogger.logEnabled = openLog;
        }

        public void OpenFileCallback(string blob)
        {
            Client.Web.DownloadData(blob).Execute(OnDownloadAssetDataCompleted);
            void OnDownloadAssetDataCompleted(Runnable<byte[]> executed)
            {
                if (!executed.EnsureExecutedSuccessful())
                {
                    Client.Events.Notify(EventNames.ERROR_MESSAGE_NOTICE, string.Format(ErrorInfo.LOAD_FILE_FAIL));
                    return;
                }
                Client.Events.Notify(EventNames.OPEN_FILE_COMPLATED, executed.result);
            }
        }

        /// <summary>
        /// 初始化编辑器
        /// </summary>
        /// <param name="args">初始化参数</param>
        public void Initialized(string args) => Client.Avatar.Initialized(Newtonsoft.Json.JsonConvert.DeserializeObject<AvatarOptions>(args));

        /// <summary>
        /// 设置相机控制方式
        /// </summary>
        /// <param name="type">-1:无控制，在绘画时切换到-1，0：移动视图 1：旋转试图 2：缩放视图</param>
        public void SetSceneControllerType(int type) => CameraCtrl.instance.SetControllrtType((ControllerState)type);

        /// <summary>
        /// 清理部件
        /// </summary>
        /// <param name="element"></param>
        public void ClearElement(int element) => Client.Avatar.ClearElement(element);

        /// <summary>
        /// 将部位显示在视图中心
        /// </summary>
        public void ShowInView(int element) => Client.Avatar.ShowInView(element);

        /// <summary>
        /// 导出配置
        /// </summary>
        /// <param name="configName">保存的配置名</param>
        public void ExportConfig(string configName) => Client.Avatar.ExportConfig(configName);

        /// <summary>
        /// 获取部件数据
        /// </summary>
        /// <param name="element">部位枚举</param>
        public void GetElementData(int element) => Client.Avatar.GetElementData(element);


        /// <summary>
        /// 导入配置
        /// </summary>
        /// <param name="config">配置数据</param>
        public void ImportConfig(string config) => Client.Avatar.ImportConfig(config);

        /// <summary>
        /// 设置部件模型
        /// </summary>
        /// <param name="elementData">部位数据</param>
        public void SetElementData(string elementData) => Client.Avatar.SetElementData(Newtonsoft.Json.JsonConvert.DeserializeObject<List<ElementData>>(elementData));

        /// <summary>
        /// 合并Avatar
        /// </summary>
        public void Combine() => Client.Avatar.Combine();

        /// <summary>
        /// 上传部件资源
        /// </summary>
        /// <param name="element">部件位置</param>
        /// <param name="fileDataString">文件数据</param>
        public void UploadAsset(int element) => Client.Avatar.UploadAsset(element);

        /// <summary>
        /// 预览
        /// </summary>
        /// <param name="element">部位枚举</param>
        public void PreviewAsset(int element) => Client.Avatar.PreviewAsset(element);

        /// <summary>
        /// 隐藏部位
        /// </summary>
        /// <param name="element"></param>
        public void DisableElement(int element) => Client.Avatar.DisableElement(element);

        /// <summary>
        /// 显示部位
        /// </summary>
        /// <param name="element"></param>
        public void EnableElement(int element) => Client.Avatar.EnableElement(element);

        /// <summary>
        /// 部件涂鸦
        /// </summary>
        /// <param name="element"></param>
        public void ElementGraffiti(string data) => Client.Drawing.ElementGraffiti(data);

        /// <summary>
        /// 保存涂鸦数据
        /// </summary>
        /// <param name="name">文件名,如果文件为空则不保存数据</param>
        public void Save(string name) => Client.Drawing.Save(name);

        /// <summary>
        /// 发布涂鸦
        /// </summary>
        public void PublishGraffiti(string name) => Client.Drawing.PublishGraffiti(name);

        /// <summary>
        /// 设置画笔
        /// </summary>
        /// <param name="brush">1:钢笔 2:刷子 3:橡皮檫 4:拖动</param>
        public void SetPaintbrushType(int brush) => Client.Drawing.SetPaintbrushType(brush);

        /// <summary>
        /// 设置画笔颜色
        /// </summary>
        /// <param name="hexadecimal">颜色值</param>
        public void SetPaintbrushColor(string hexadecimal) => Client.Drawing.SetPaintbrushColor(hexadecimal);

        /// <summary>
        /// 设置画笔大小
        /// </summary>
        /// <param name="width"></param>
        public void SetPaintbrushWidth(float width) => Client.Drawing.SetPaintbrushWidth(width);

        /// <summary>
        /// 在当前选中的图层中导入涂鸦图片
        /// </summary>
        public void ImportGraffitiTexture() => Client.Drawing.ImportGraffitiTexture();

        /// <summary>
        /// 新建图层
        /// </summary>
        /// <param name="name">图层名</param>
        public void NewLayer(string name) => Client.Drawing.NewLayer(name);

        /// <summary>
        /// 选中图层
        /// </summary>
        /// <param name="name">要选中的图层名</param>
        public void SelectionLayer(string name) => Client.Drawing.SelectionLayer(name);

        /// <summary>
        /// 删除选中图层
        /// </summary>
        public void DeleteLayer() => Client.Drawing.DeleteLayer();

        /// <summary>
        /// 设置选中图层透明度
        /// </summary>
        /// <param name="alpha">透明度 </param>
        public void SetLayerAlpha(float alpha) => Client.Drawing.SetLayerAlpha(alpha);

        /// <summary>
        /// 设置图层缩放大小
        /// </summary>
        /// <param name="size"></param>
        public void SetLayerSize(float size) => Client.Drawing.SetLayerSize(size);

        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="isBackup">0:后退，1:前进</param>
        public void Undo(int isBackup) => Client.Drawing.Undo(isBackup);

        /// <summary>
        /// 设置图层排序
        /// </summary>
        /// <param name="index"></param>
        public void SetSort(int index) => Client.Drawing.SetSort(index);
    }
}